package com.hhkj.admin.question.service.extend.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.admin.question.common.converter.QuestionCollectionConverter;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.dopo.QuestionDo;
import com.hhkj.admin.question.model.dopo.QuestionPo;
import com.hhkj.admin.question.model.req.QuestionReq;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.admin.question.service.base.QuestionService;
import com.hhkj.admin.question.service.extend.QuestionExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * QuestionServiceExtendImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/8 16:00
 */
@Service
public class QuestionExtendServiceImpl
        extends BaseExtendServiceImpl<Question, QuestionDo, QuestionPo, QuestionService>
        implements QuestionExtendService
{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchGroup(List<QuestionReq> questionGroupList)
    {
        List<QuestionReq> questionReqList = QuestionCollectionConverter.groupReqToList(questionGroupList);
        List<Question> questionDoList = new ArrayList<>(questionReqList);
        boolean result = this.getService().saveBatch(questionDoList);
        if (result) {
            List<Question> childrenQuestionList = new ArrayList<>();
            for (QuestionReq questionParent : questionReqList) {
                if (CollectionUtil.isEmpty(questionParent.getQuestionList())) {
                    continue;
                }
                for (QuestionReq questionChild : questionParent.getQuestionList()) {
                    questionChild.setParentId(questionParent.getId());
                    childrenQuestionList.add(questionChild);
                }
            }
            if (CollectionUtil.isNotEmpty(childrenQuestionList)) {
                this.getService().updateBatchById(childrenQuestionList);
            }
        }
        return result;
    }

    @Override
    public List<QuestionRes> listResGroupByBankIdList(List<Long> bankIdList)
    {
        QuestionDo questionDo = new QuestionDo();
        questionDo.setBankIdList(bankIdList);
        List<QuestionPo> questionList = this.getService().listPoByDo(questionDo);
        return QuestionCollectionConverter.listToListGroup(BeanUtil.copyToList(questionList, QuestionRes.class));
    }

    @Override
    public List<QuestionRes> listPoGroupByBankId(Long bankId)
    {
        QuestionDo questionDo = new QuestionDo();
        questionDo.setBankId(bankId);
        List<QuestionPo> questionList = this.getService().listPoByDo(questionDo);
        return QuestionCollectionConverter.listToListGroup(BeanUtil.copyToList(questionList, QuestionRes.class));
    }
}
